const express = require('express')
const route = express.Router()
const adminModel = require('../db/model/adminModel')

//引入权限控制模块
const isLoginMiddle  = require('../middleware/isLoginMiddle')

route.delete('/deleteAdmin',isLoginMiddle, async (req, res) => {
  const { adminName } = req.query
  // console.log(req.query);
  //根据前端传来的adminName 在数据库中删除这个数据
  //删除这个数据时会返回一个对象,对象内有一个deletedCount属性为删除的个数
  try {
    const deleteResult = await adminModel.deleteOne({
      adminName
    })

    if (deleteResult.deletedCount > 0) {
      return res.send({
        code: 10000,
        msg: "删除管理员" + adminName + "成功",
        data: null
      })
    }
    res.send({
      code: 10001,
      msg: "删除管理员" + adminName + "失败",
      data: null
    })
  } catch (e) {
    res.send({
      code: 10001,
      msg: "删除管理员失败" + e.message,
      data: null
    })
  }
})

module.exports = route